package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbChannelConfig;
import com.mb.app.domain.TbUserInfo;
import com.mb.app.dao.TbUserInfoMapper;
import com.mb.app.service.ITbChannelConfigService;
import com.mb.app.service.ITbUserInfoService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-03-21
 */
@Service
@Slf4j
public class TbUserInfoServiceImpl extends ServiceImpl<TbUserInfoMapper, TbUserInfo> implements ITbUserInfoService {

    @Autowired
    private ITbChannelConfigService channelConfigService;

    @Override
    public List<TbUserInfo> queryList(Map<String, Object> map) {
        List<TbUserInfo> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.count(map);
    }


    @Override
    public int remove(Long userId) {
//        return this.baseMapper.deleteById(appId);
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setUserId(userId);
        tbUserInfo.setDataStatus(0);
        return this.baseMapper.updateById(tbUserInfo);
    }

    @Override
    public int status(Long userId,Integer userstatus) {
//        return this.baseMapper.deleteById(appId);
        TbUserInfo tbUserInfo = new TbUserInfo();
        tbUserInfo.setUserId(userId);
        tbUserInfo.setUserStatus(userstatus);
        return this.baseMapper.updateById(tbUserInfo);
    }

    @Override
    public List<TbUserInfo> queryListChannel(Map<String, Object> map) {
        int total = this.baseMapper.countChannel(map);
        TbChannelConfig channelConfig = channelConfigService.getChannelByUserId((Long)map.get("userId"));
        if(null != channelConfig && null != channelConfig.getChannelId()) {
            map.put("regChannelId",channelConfig.getChannelId());
            //设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
            BigDecimal bd = channelConfig.getDeductRate().setScale(0, BigDecimal.ROUND_HALF_DOWN);
            //转化为字符串输出
            double deductRate = Double.parseDouble(bd.toString());
            double tt = total * (1-deductRate/100);
            total = (new Double(tt)).intValue();
            int pageNumber = Integer.parseInt((String) map.get("pageNumber"));
            int pageSize = Integer.parseInt((String)map.get("pageSize"));
            if (pageNumber*pageSize > total) {
                int limit = total - (pageNumber-1)*pageSize;
                log.info("limit==",limit);
                map.put("limit",limit);
            }

        }
        List<TbUserInfo> list = this.baseMapper.listChannel(map);
        return list;
    }

    @Override
    public int countChannel(Map<String, Object> map) {
        int total = this.baseMapper.countChannel(map);
        TbChannelConfig channelConfig = channelConfigService.getChannelByUserId((Long)map.get("userId"));
        if(null != channelConfig && null != channelConfig.getChannelId()) {
            map.put("regChannelId",channelConfig.getChannelId());
            //设置小数位数，第一个变量是小数位数，第二个变量是取舍方法(四舍五入)
            BigDecimal bd = channelConfig.getDeductRate().setScale(0, BigDecimal.ROUND_HALF_DOWN);
            //转化为字符串输出
            double deductRate = Double.parseDouble(bd.toString());
            double tt = total * (1-deductRate/100);
            total = (new Double(tt)).intValue();
        }
        return total;
    }

    @Override
    public TbUserInfo getDtl(Long userId) {
        return this.baseMapper.selectById(userId);
    }

}
